package com.atguigu.petstore.sys.controller;

import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.sys.entity.SysMenu;
import com.atguigu.petstore.sys.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-09-21 14:15
 */

@Tag(name = "系统菜单管理")
@RestController
@RequestMapping("/api/sys")
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;

    @Operation(summary = "查询所有系统菜单")
    @GetMapping("/menu")
    public Result getAllMenu(){
        List<SysMenu> list = sysMenuService.list();
        return Result.ok(list);
    }

    @Operation(summary = "获取菜单树")
    @GetMapping("/menu/tree")
    public Result getMenuTree(){

        //返回所有父菜单集合，并且每个父菜单都封装了子菜单
        List<SysMenu> menus = sysMenuService.getMenuTree();
        return Result.ok(menus);
    }



    @Operation(summary = "添加菜单")
    @PostMapping("/menu")
    public Result addMenu(@RequestBody SysMenu sysMenu){

        sysMenuService.save(sysMenu);
        return Result.ok();
    }

    @Operation(summary = "修改菜单")
    @PutMapping("/menu")
    public Result updateMenu(@RequestBody SysMenu menu){

        sysMenuService.updateById(menu);
        return Result.ok();
    }


    @Operation(summary = "删除菜单")
    @DeleteMapping("/menu/{id}")
    public Result delete(@PathVariable("id") Long id){

        sysMenuService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "查询单个菜单")
    @GetMapping("/menu/{id}")
    public Result getById(@PathVariable("id") Long id){

        SysMenu menu = sysMenuService.getById(id);
        return Result.ok(menu);
    }
}
